﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SBACN.WebSite.Models;
using SBACN.WebSite.Code;
using SBACN.WebSite.Common;
using System.Linq.Expressions;

namespace SBACN.WebSite.Admin.Committee
{
	public partial class Communication : AdminBasePage
	{
		/// <summary>
		/// 系统字典类型
		/// </summary>
		public string SystemType
		{
			get
			{
				//if (ViewState["SystemType"] == null)
				//    return "000300010004";//通讯录默认值
				//else
				//    return ViewState["SystemType"].ToString();
				return this.SystemCode;
			}
			private set
			{
				ViewState["SystemType"] = this.SystemCode;
			}
		}

		public string FileLoad
		{
			get;
			set;
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			Bindlink();
		}

		protected void Bindlink()
		{
			Expression<Func<TB_Professional, bool>> f1 = (p => p.Type == this.SystemType);
			var resultP = DAL.DALHelper.Instance.GetList<TB_Professional>(f1);
			if (resultP.Count > 0)
			{
				Expression<Func<TB_UploadInfo, bool>> fun = (p => p.Type == this.SystemType);
				Expression<Func<TB_UploadInfo, DateTime>> o = (p => p.CreateDate);
				var result = DAL.DALHelper.Instance.GetList<TB_UploadInfo, DateTime>(fun, o, false).FirstOrDefault();
				if (result == null)
				{
					this.FileLoad = "javascript:void(0);";
				}
				else
				{
					this.FileLoad = result.FilePath;
				}
			}
			else
			{
				this.FileLoad = "javascript:void(0);";
			}
			

		}

		protected void Subbtn_Click(object sender, EventArgs e)
		{

			if (this.FileUp.FileBytes.Length == 0 || this.FileUp.FileBytes.Length > (MaxUploadSize * 1024 * 1024))
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script type='text/javascript'>alert('上传的文件大小不符合系统设置,请重新上传！');window.location=location;</script>");
				return;
			}

			string extension = GetFileExt(this.FileUp.FileName);
			if (("," + UploadFileTypes + ",").ToLower().IndexOf(("," + extension + ",").ToLower()) < 1)
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script type='text/javascript'>alert('上传的文件格式不正确,请重新上传！');window.location=location;</script>");
				return;
			}

			string Url = BasePage.UploadFile(this.SystemType, this.FileUp.FileName, this.FileUp.FileBytes);
			TB_UploadInfo upload = DAL.DALHelper.Instance.Create<TB_UploadInfo>();
			upload.CreateBy = AdminInfo.UserName;
			upload.CreateDate = DateTime.Now;
			upload.FilePath = Url;
			upload.FileType = BasePage.GetFileExt(this.FileUp.FileName);
			upload.Name = "正副主任单位上传";
			upload.Type = this.SystemType;
			DAL.DALHelper.Instance.Add<TB_UploadInfo>(upload);
			DAL.DALHelper.Instance.SaveChanges();

			// 删除已有通讯录
			Expression<Func<TB_Professional, bool>> f1 = (p => p.Type == this.SystemType);
			var resultP = DAL.DALHelper.Instance.GetList<TB_Professional>(f1);
			if (resultP.Count > 0 )
			{
				foreach (var p in resultP)
				{
					DAL.DALHelper.Instance.Delete<TB_Professional>(p);
				}
				DAL.DALHelper.Instance.SaveChanges();
			}

			// 读文档保存数据
			string sheel = "Sheet1";
			int row;

			var DI = ExcelAccess.GetExcelRows(Server.MapPath(Url), sheel);
			if (DI.Tables[0].Columns.Count == 2)
			{
				row = DI.Tables[0].Rows.Count;

				int ef = 0;
				bool bool_ef = false;
				int mf = 0;
				bool bool_mf = false;
				for (int j = 0; j < 2; j++)
				{
					string ExlName = DI.Tables[0].Columns[j].ToString();
					switch (ExlName)
					{
						case "职务":
							ef = j;
							bool_ef = true;
							break;
						case "单位":
							mf = j;
							bool_mf = true;
							break;
					}
				}

				if (!bool_ef || !bool_mf )
				{
					ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script>alert('上传文件格式不正确，请重新上传！');window.location=location;</script>");
					return;
				}

				for (int i = 0; i < row; i++)
				{
					string zw = DI.Tables[0].Rows[i][ef].ToString();
					string dw = DI.Tables[0].Rows[i][mf].ToString();

					var NewObj = DAL.DALHelper.Instance.Create<TB_Professional>();
					NewObj.Level = zw;
					NewObj.Type = this.SystemType;
					NewObj.MemberName = dw;
					DAL.DALHelper.Instance.Add<TB_Professional>(NewObj);
				}

				DAL.DALHelper.Instance.SaveChanges();
				ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script type='text/javascript'>alert('通讯录上传成功！');window.location=location;</script>");
				return;
			}
			else
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script type='text/javascript'>alert('上传文件格式不正确，请重新上传！');window.location=location;</script>");
				return;
			}


		}


	}
}